//
// Created on 2024/10/15.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include "createPixelMap.h"

napi_value createPixelMap(napi_env env, const std::vector<uint8_t> &byteArray) {
    //创建ImageSource实例
    OH_ImageSourceNative *source = nullptr;
    Image_ErrorCode errCode = OH_ImageSourceNative_CreateFromData(const_cast<uint8_t*>(byteArray.data()), byteArray.size(), &source);
     if (errCode != IMAGE_SUCCESS) {
           OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_CreateFromUri failed, errCode: %{public}d.", errCode);
           return nullptr;
       }
    //创建定义图片信息的结构体对象，并获取图片信息
       OH_ImageSource_Info *imageInfo;
       OH_ImageSourceInfo_Create(&imageInfo);
       errCode = OH_ImageSourceNative_GetImageInfo(source, 0, imageInfo);
       if (errCode != IMAGE_SUCCESS) {
           OH_LOG_ERROR(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo failed, errCode: %{public}d.", errCode);
        return nullptr;
       };
    
        uint32_t width;
        OH_ImageSourceInfo_GetWidth(imageInfo, &width);
           OH_LOG_INFO(LOG_APP, "ImageSourceNativeCTest sourceTest OH_ImageSourceNative_GetImageInfo failed, errCode: %{public}d.", width);
    
            uint32_t height;
        OH_ImageSourceInfo_GetHeight(imageInfo, &height);
    //创建参数结构体实例，并设置参数
       OH_DecodingOptions *createOpts;
    OH_DecodingOptions_Create(&createOpts);
        Image_Size size;
        size.width = width;
        size.height = height;
        OH_DecodingOptions_SetDesiredSize(createOpts, &size);

       //创建Pixelmap实例
       OH_PixelmapNative *pixelmap = nullptr;
       Image_ErrorCode errCodeP = OH_ImageSourceNative_CreatePixelmap(source, createOpts, &pixelmap);
        napi_value pixelmapNapi;
    OH_PixelmapNative_ConvertPixelmapNativeToNapi(env, pixelmap, &pixelmapNapi);
    return pixelmapNapi;
};


std::string TransformByte(const std::string &input) {       
    std::string encoded;  
    size_t i = 0, j = 0;  
    uint8_t byte3[3] = {0};  
    uint8_t byte4[4] = {0};
    
    // 遍历输入字符串中的每个字符  
    for (char byte : input) {  
        byte3[i++] = static_cast<uint8_t>(byte); // 假设输入是ASCII  
        if (i == 3) {  
            byte4[0] = (byte3[0] & 0xfc) >> 2;  
            byte4[1] = ((byte3[0] & 0x03) << 4) | ((byte3[1] & 0xf0) >> 4);  
            byte4[2] = ((byte3[1] & 0x0f) << 2) | ((byte3[2] & 0xc0) >> 6);  
            byte4[3] = byte3[2] & 0x3f;
            
            // 添加编码后的字符到结果字符串  
            for (int k = 0; k < 4; k++) {  
                encoded += base64_chars[byte4[k]];  
            }  
            i = 0;  
        }
    }  
    
    // 处理剩余字符（如果有）  
    if (i != 0) {  
        for (size_t k = i; k < 3; k++) {  
            byte3[k] = 0; // 填充剩余字节为0  
        }  
    
        // 执行编码，类似于前面的处理  
        byte4[0] = (byte3[0] & 0xfc) >> 2;  
        byte4[1] = ((byte3[0] & 0x03) << 4) | ((byte3[1] & 0xf0) >> 4);  
        byte4[2] = ((byte3[1] & 0x0f) << 2) | ((byte3[2] & 0xc0) >> 6);
        
        // 添加编码后的字符到结果字符串  
        for (size_t k = 0; k < i + 1; k++) {  
            encoded += base64_chars[byte4[k]];  
        }  
    
        // 添加'='以填充到4的倍数  
        while (i++ < 3) {  
            encoded += '=';  
        }  
    }
 
    return encoded;  
}